home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_5 / issue_06 / benchmarks / c_source / lofile < prev    next >
Encoding:
Text File  |  1991-06-04  |  1.8 KB  |  97 lines

  1. /*
  2.  * File IO benchmark
  3.  */
  4.  
  5. #include "timer.h"
  6. #define ERROR -1
  7. #define READERR 0
  8. #define BEG 0
  9. #define CURR 1
  10. #define END 2
  11. #define READ 0
  12. #define WRITE 1 
  13. #define UPDATE 2
  14.  
  15. #define OKCLOSE 0
  16. #define FILESIZE 650001
  17. #define COUNT 500
  18.  
  19. #define C 13849L
  20. #define A 25173L
  21. #define ODDNUM 23
  22.  
  23. long seed=7L;
  24. long random(), lseek();
  25. main()
  26. {
  27.     int    i;
  28.     long    j,pos;
  29.     int fd;
  30.     char     buffer[ODDNUM + 1];
  31.  
  32.         init_timer();
  33.     start_timer();
  34.     if((fd=creat("test.dat",0666)) == ERROR)
  35.         abort("Can't write data file\n");
  36.     else
  37.         printf("File opened for sequential writing\n");
  38.  
  39.     for(j = 0; j < FILESIZE; ++j)
  40.         if(write(fd, "x", 1) == ERROR)
  41.             abort("Unexpected EOF in writing data file\n");
  42.  
  43.     if(close(fd) != OKCLOSE)
  44.         abort("Error closing data file\n");
  45.     else
  46.         printf("Normal termination writing data file\n");
  47.     if((fd = open("test.dat", UPDATE)) == ERROR)
  48.         abort("Can't open data file for random reading and writing\n");
  49.     else
  50.         printf("File opened for random reading and writing\n");
  51.  
  52.     for(i = 0; i < COUNT; ++i)
  53.     {
  54.         j = random(FILESIZE);
  55.         if(j < 0L)
  56.             j = (-j);
  57.         if(FILESIZE - j < ODDNUM)
  58.             continue;
  59.         if((pos = lseek(fd, j, BEG)) == -1L)
  60.             abort("Error seeking to random offset\n");
  61.         if(read(fd, buffer, ODDNUM) == READERR)
  62.             abort("Error reading at random offset\n");
  63.  
  64.         j = random(FILESIZE);
  65.         if(j < 0L)
  66.             j = (-j);
  67.         if(FILESIZE - j < ODDNUM)
  68.             continue;
  69.         if((pos = lseek(fd, j, BEG)) == -1L)
  70.             abort("Error seeking to random offset\n");
  71.         if(write(fd, buffer, ODDNUM) == READERR)
  72.             abort("Error writing at random offset\n");
  73.         }
  74.  
  75.     if(close(fd) != OKCLOSE)
  76.         abort("Error closing data file\n");
  77.     else
  78.         printf("Normal terminations from reading and writing\n");
  79.     print_elapsed("lofile benchmark", REALMIN);
  80.         exit(0);
  81. }
  82.  
  83. long random(size)
  84. long size;
  85. {
  86.     seed = seed * A+C;
  87.     return (seed % size);
  88. }
  89.  
  90. abort(message)
  91. char *message;
  92. {
  93.     printf(message);
  94.     exit(ERROR);
  95. }
  96.  
  97.